<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
	<title>出入库信息管理</title>
	<meta name="decorator" content="default"/>
	<style type="text/css">
		.w250{
			width: 250px !important;
		}
	</style>
	<script type="text/javascript">
		$(document).ready(function() {
			 init();
			 initTable();
			});
		   var areaData;
		function init(){
				 var url="${pageContext.request.contextPath}/a/area/areaChoose/getByArea";
					$.ajax({ 
						url: url,
						type: "GET",
						dataType:"json",
						cache: false,
						async:false,
						success: function(data){
							areaData=data;
				        },
				        error:function(){
				        	return ;
				        }
					});
				
			}
			
			function choose(obj,code,type,id,parentIds,grade){
			    debugger;
			 	var str=$(obj).parent().find("a").text();
			 	parent.layer.closeAll();
			 	parent.setReceive(str,code,type,id,parentIds,grade);
			}
			
		function getAreaByFatherCode(id,obj){
			var data=[];
			if(areaData.length==0){
				return ;
			}else{
				for(var i in areaData){  
					console.info(areaData[i].parentId);
					if(areaData[i].parentId==id)
		     		  data.push(areaData[i]);
		    		}  
			}
			if(data.length==0){
				return ;
			}else{
				var name=getName(id);
				$(".area").empty();
				$(".area").append('<li><a href="#"  onclick="initTable()">地区选择:</a></li>');
				$(".area").append('<li><a href="#"  onclick="initTable(\''+id+'\',this)">'+name+'</a></li>');
		
			}
			var buffer = new StringBuffer();
			for(var i=0;i<data.length;i++){
				buffer.append('<div class="control-group "> ');
				if(data[i].sendOrReceive=="0" || data[i].sendOrReceive=="1"||data[i].sendOrReceive=="3"){
					buffer.append('<a class="control-label w250" onclick="getAreaByFatherCode(\''+data[i].id+'\',this)" style="cursor: pointer;">'+data[i].name+'</a>&nbsp;&nbsp;<input  class="btn btn-primary" onclick="choose(this,\''+data[i].code+'\',\''+data[i].type+'\',\''+data[i].id+'\',\''+data[i].parentIds+'\',\''+data[i].grade+'\')" type="button" value="选择">');
				}else{
					buffer.append('<a class="control-label w250" onclick="getAreaByFatherCode(\''+data[i].id+'\',this)" style="cursor: pointer;">'+data[i].name+'</a>');
				}
				buffer.append('</div>');
			}
			$(".content").empty();
			$(".content").append(buffer.toString());
		
		
		}
		
		function StringBuffer() {
		     this.__strings__ = new Array();
		 }
		 StringBuffer.prototype.append = function (str) {
		     this.__strings__.push(str);
		     return this;    //方便链式操作
		}
		StringBuffer.prototype.toString = function () {
		     return this.__strings__.join("");
		 }
		
		
		function initTable(id){
			var pid=getParentId(id);
			var pname=getParentName(id);
			var data=[];
			if(areaData.length==0){
				return ;
			}else{
				if(id){
					$(".area").empty();
					$(".area").append('<li><a href="#" onclick="initTable()">地区选择:</a></li>');
					if(pid&&pname){
						$(".area").append('<li><a href="#"  onclick="initTable(\''+pid+'\')">'+pname+'</a></li>');
						for(var i in areaData){  
							if(areaData[i].parentId==pid) 
				     		  data.push(areaData[i]);
				    		} 
					}else{
		     			data.push(areaData[0]);
					}
				}else{
					return getAreaByFatherCode('${officeId}',this);
				}
			}
			if(data.length==0){
				return ;
			}
			var buffer = new StringBuffer();
			for(var i=0;i<data.length;i++){
				buffer.append('<div class="control-group "> ');
				if(data[i].sendOrReceive=="0" || data[i].sendOrReceive=="1" || data[i].sendOrReceive=="3"){
					buffer.append('<a class="control-label w250" onclick="getAreaByFatherCode(\''+data[i].id+'\',this)" style="cursor: pointer;">'+data[i].name+'</a>&nbsp;&nbsp;<input  class="btn btn-primary " onclick="choose(this,\''+data[i].code+'\',\''+data[i].type+'\',\''+data[i].id+'\',\''+data[i].parentIds+'\',\''+data[i].grade+'\')" type="button" value="选择">');
				}else{
					buffer.append('<a class="control-label w250" onclick="getAreaByFatherCode(\''+data[i].id+'\',this)" style="cursor: pointer;">'+data[i].name+'</a>');
				}
				buffer.append('</div>');
			}
			$(".content").empty();
			$(".content").append(buffer.toString());
			
		}
		function getParentId(id){
			for(var i in areaData){  
				if(areaData[i].id==id)
		    		  return areaData[i].parentId;
		   		}
			return "";
		}
		function getParentName(id){//根据id获取父节点的名称
			for(var i in areaData){  
				if(areaData[i].id==id)
					for(var k in areaData){
						if(areaData[k].id==areaData[i].parentId){
		    		  			return areaData[k].name;
						}
					}
		   		}
			return "";
		}
		function getName(id){//根据ID获取对象的名称
			for(var i in areaData){  
				if(areaData[i].id==id)
		    		  	return areaData[i].name;
					}
			return "";
		}
	</script>
	 
</head>
<body>
	<ul class="nav nav-tabs area">
	</ul><br/>
	<form:form  class="form-horizontal">
		<sys:message content="${message}"/>		
		<div class="content">
		
		</div>
	</form:form>
</body>
</html>